package com.bcqn.controller;

import com.alibaba.fastjson.JSONObject;
import com.bcqn.common.CommonResponse;
import com.bcqn.model.HousingInformation;
import com.bcqn.model.vo.RentingQueryObj;
import com.bcqn.service.RentingService;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import java.sql.Timestamp;
import java.util.Date;


/**
 * @author liugang
 * @version 1.0
 * @date 2020/4/17 16:32
 */
@RestController
@RequestMapping("/renting")
@Slf4j
public class RentingController extends CommonController{

    @Resource
    private RentingService rentingService;


    /**
     * 发布租房信息
     * @param housingInformation
     * @return
     */
    @PostMapping("/add")
    public CommonResponse add(@RequestBody HousingInformation housingInformation){
        if(housingInformation!=null) {
            log.info("添加的对象是param,housingInformation={}", JSONObject.toJSONString(housingInformation));
            return CommonResponse.success(rentingService.add(getUserId().intValue(),housingInformation));
        }else {
            return CommonResponse.fail("传入的对象为空");
        }
    }

    /**
     * 租房的详情
     * @param id 方源的id属性
     * @return
     */
    @RequestMapping("/detail")
    public  CommonResponse detail(Long id){
        if (id!=null){
            log.info("添加的对变量是param,id={}", JSONObject.toJSONString(id));
            HousingInformation housingInformation = rentingService.detail(id);
            return CommonResponse.success(housingInformation);
        } else {
            return CommonResponse.fail("传入的变量为空");
        }

    }

    /**
     * 查询用户对于的数据的列表
     * @param rentingQueryObj 查询条件
     * @return
     */
    @RequestMapping("/list")
    public CommonResponse list(@RequestBody RentingQueryObj rentingQueryObj,@RequestParam(defaultValue = "1") int pageNO,int pageSize){
        if(rentingQueryObj != null) {
            log.info("添加的对变量是param,rentingQueryObj={}", JSONObject.toJSONString(rentingQueryObj));
            //利用分页插件来传递其相关的查询的信息
            PageInfo<HousingInformation> pageInfo = rentingService.getList(rentingQueryObj,pageNO,pageSize);
            return CommonResponse.success(pageInfo);
        } else {
            return CommonResponse.fail("查询条件为空,无法进行相关的信息的查询");
        }
    }

    /**
     * 收藏房间信息
     * @param houingId 收藏房源的id信息
     * @param action
     * @return
     */
    @RequestMapping("/collect")
    public CommonResponse collect(int houingId,String action){
        log.info("添加的对变量是param,houingId={},action={}", JSONObject.toJSONString(houingId),JSONObject.toJSONString(action));
        if(action != null){
            if(action.equals("add")){
                rentingService.collect(getUserId().intValue(),houingId);
                return CommonResponse.success();
            }
            if(action.equals("del")){
                rentingService.delCollect(getUserId().intValue(), houingId);
                return CommonResponse.success();
            }
        }
        return CommonResponse.fail("该操作信息传递为空");
    }

    /**
     * 删除发布的租房的信息
     * @param id
     * @return
     */
    @PutMapping("/delete")
    public CommonResponse delete(Integer id){
        if (id!=null){
            log.info("添加的对象是param,id={}", JSONObject.toJSONString(id));
            rentingService.delete(id);
            return CommonResponse.success();
        } else {
            return CommonResponse.fail("传递的数据为空,请输入正确的id信息");
        }

    }
    /**
     * 获取我的租房的列表的
     * @param userId
     * @param pageSize
     * @param currentPage
     * @return
     */
    @RequestMapping("/myRentingCollection")
    public CommonResponse myRentingCollection(@RequestParam("pageSize") int pageSize, @RequestParam(defaultValue = "1") int currentPage){
        return CommonResponse.success(rentingService.myRentingCollection(getUserId(),pageSize,currentPage));
    }

    @RequestMapping("/getMyPost")
    public CommonResponse getMyPost(@RequestParam("pageSize") int pageSize, @RequestParam("currentPage") int currentPage){
        return CommonResponse.success(rentingService.getMyPost(getUserId(),pageSize,currentPage));
    }

    @RequestMapping("/getMyCollection")
    public CommonResponse getMyCollection(@RequestParam("pageSize") int pageSize, @RequestParam("currentPage") int currentPage){
        return CommonResponse.success(rentingService.getMyCollection(getUserId(),pageSize,currentPage));
    }
}
